package com.apex.sdk.ui;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.SosCantactCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleSosContact;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivitySosBinding;
import com.apex.sdk.dialog.WaitingDialog;

public class SosActivity extends AppCompatActivity {
    ActivitySosBinding activitySosBinding;
    final String TAG = this.getClass().getSimpleName();
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x11) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                EABleSosContact eaBleSosContact = (EABleSosContact) msg.obj;
                if (eaBleSosContact != null) {
                    if (!TextUtils.isEmpty(eaBleSosContact.getcName())) {
                        activitySosBinding.name1.setText(eaBleSosContact.getcName());
                    }
                    if (!TextUtils.isEmpty(eaBleSosContact.getcNum())) {
                        activitySosBinding.num1.setText(eaBleSosContact.getcNum());
                    }
                    if (!TextUtils.isEmpty(eaBleSosContact.getEx_name())) {
                        activitySosBinding.name2.setText(eaBleSosContact.getEx_name());
                    }
                    if (!TextUtils.isEmpty(eaBleSosContact.getEx_num())) {
                        activitySosBinding.num2.setText(eaBleSosContact.getEx_num());
                    }
                    if (!TextUtils.isEmpty(eaBleSosContact.getThird_name())) {
                        activitySosBinding.name3.setText(eaBleSosContact.getThird_name());
                    }
                    if (!TextUtils.isEmpty(eaBleSosContact.getThird_num())) {
                        activitySosBinding.num3.setText(eaBleSosContact.getThird_num());
                    }
                }
            } else if (msg.what == 0x12) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                boolean result = (boolean) msg.obj;
                if (!result) {
                    Toast.makeText(SosActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
                }
            } else if (msg.what == 0x13) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(SosActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x15) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(SosActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            }
        }
    };
    WaitingDialog waitingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySosBinding = ActivitySosBinding.inflate(getLayoutInflater());
        setContentView(activitySosBinding.getRoot());
        activitySosBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activitySosBinding.tool);
        getSupportActionBar().setTitle(null);
        activitySosBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activitySosBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(SosActivity.this);
                    }
                    waitingDialog.show();
                    EABleSosContact eaBleSosContact = new EABleSosContact();
                    eaBleSosContact.setcName(activitySosBinding.name1.getText().toString());
                    eaBleSosContact.setcNum(activitySosBinding.num1.getText().toString());
                    eaBleSosContact.setEx_name(activitySosBinding.name2.getText().toString());
                    eaBleSosContact.setEx_num(activitySosBinding.num2.getText().toString());
                    eaBleSosContact.setThird_name(activitySosBinding.name3.getText().toString());
                    eaBleSosContact.setThird_num(activitySosBinding.num3.getText().toString());
                    Log.e(TAG, "传递过去的数据:" + JSON.toJSONString(eaBleSosContact));
                    EABleManager.getInstance().setSos(eaBleSosContact, new GeneralCallback() {
                        @Override
                        public void result(boolean b) {
                            Log.e(TAG, "添加sos成功");
                            if (mHandler != null) {
                                Message message = new Message();
                                message.what = 0x12;
                                message.obj = b;
                                mHandler.sendMessage(message);
                            }

                        }

                        @Override
                        public void mutualFail(int i) {
                            Log.e(TAG, "添加sos失败");
                            if (mHandler != null) {
                                Message message = new Message();
                                message.what = 0x13;
                                message.obj = i;
                                mHandler.sendMessage(message);
                            }
                        }
                    });
                }

            }
        });
        if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(SosActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.sos, new SosCantactCallback() {
                @Override
                public void sosInfo(EABleSosContact eaBleSosContact) {
                    if (eaBleSosContact != null) {
                        Log.e(TAG, "查询sos成功:" + JSON.toJSONString(eaBleSosContact));
                        if (mHandler != null) {
                            Message message = new Message();
                            message.what = 0x11;
                            message.obj = eaBleSosContact;
                            mHandler.handleMessage(message);
                        }
                    }

                }

                @Override
                public void mutualFail(int i) {
                    Log.e(TAG, "查询sos失败");
                    if (mHandler != null) {
                        Message message = new Message();
                        message.what = 0x15;
                        mHandler.handleMessage(message);
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        super.onDestroy();
    }
}
